package com.shuangti.food.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.shuangti.food.bean.FoodNutritionTbl;
import com.shuangti.food.mapper.FoodNutritionMapper;
import com.shuangti.food.service.FoodNutritionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.TimeUnit;


@Service
public class FoodNutritionServiceImpl implements FoodNutritionService {

    @Autowired
    FoodNutritionMapper foodNutritionMapper;

    @Autowired
    RedisTemplate<String, Object> redisTemplate;

    @Override
    public void addFood(FoodNutritionTbl foodNutritionTbl) {
        foodNutritionTbl.setCreated_at(LocalDateTime.now());
        foodNutritionMapper.insert(foodNutritionTbl);

    }

    @Override
    public void delFood(Long food_id) {
        foodNutritionMapper.deleteUserById(food_id);
    }

    @Override
    public boolean updateFoodInfo(FoodNutritionTbl foodNutritionTbl) {
        LambdaUpdateWrapper<FoodNutritionTbl> updateWrapper = new LambdaUpdateWrapper<>();

        updateWrapper.eq(FoodNutritionTbl::getFood_id, foodNutritionTbl.getFood_id());


        updateWrapper.set(foodNutritionTbl.getName() != null, FoodNutritionTbl::getName, foodNutritionTbl.getName())
                .set(foodNutritionTbl.getCategory() != null, FoodNutritionTbl::getCategory, foodNutritionTbl.getCategory())
                .set(foodNutritionTbl.getCalories() != 0, FoodNutritionTbl::getCalories, foodNutritionTbl.getCalories())
                .set(foodNutritionTbl.getProtein() != null, FoodNutritionTbl::getProtein, foodNutritionTbl.getProtein())
                .set(foodNutritionTbl.getFat() != null, FoodNutritionTbl::getFat, foodNutritionTbl.getFat())
                .set(foodNutritionTbl.getFiber() != null, FoodNutritionTbl::getFiber, foodNutritionTbl.getFiber())
                .set(foodNutritionTbl.getUnit() != null, FoodNutritionTbl::getUnit, foodNutritionTbl.getUnit());

        // 执行更新
        return foodNutritionMapper.update(foodNutritionTbl, updateWrapper) > 0;
    }

    @Override
    public List<FoodNutritionTbl> findByFoodName(String name) {
        LambdaQueryWrapper<FoodNutritionTbl> queryWrapper = new LambdaQueryWrapper<>();


        queryWrapper.eq(FoodNutritionTbl::getName, name);


        return foodNutritionMapper.selectList(queryWrapper);
    }

    @Override
    public String testRedis(Long food_id) {
        FoodNutritionTbl foodNutritionTbl = (FoodNutritionTbl) redisTemplate.opsForValue().get(String.valueOf(food_id));
        if(foodNutritionTbl == null) {
            foodNutritionTbl = foodNutritionMapper.selectById(food_id);

            if (foodNutritionTbl != null) {
                redisTemplate.opsForValue().set(String.valueOf(food_id), foodNutritionTbl, 10, TimeUnit.MINUTES);
            }
        }
        String result = null;
        if (foodNutritionTbl != null) {
           result  = foodNutritionTbl.toString();
        }
        return result;
    }
}
